package image;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;

import map.Properties;

import file.FileManipulator;

public class PixelImage {

	public final int TILE_SIZE = 256;
	private PixelArray pixels = new PixelArray();
	private BufferedImage blankTile;
	private ImageResizer irs;
	
	
	public PixelImage(String fileName) {
		new FileManipulator().readFileToPixelArray(pixels, fileName);
		irs = new ImageResizer(pixels);
	}
	
	public BufferedImage getBlankTile() {
		if (blankTile == null) {
			new BufferedImage(TILE_SIZE, TILE_SIZE, BufferedImage.TYPE_INT_RGB);
		}
		return blankTile;
	}
	
	public int getHeight() {
		return pixels.getHeight();
	}
	
	public int getWidth() {
		return pixels.getWidth();
	}
	
	public PixelArray getPixels() {
		return pixels;
	}
	
	/**
	 * @param x coordinate of tile.
	 * @param y coordinate of tile.
	 * @param zoom level.
	 * @return generated tile.
	 */
	public File getTile(int x, int y, int zoom) {
		File f = new File("tempTile.png");
		
		try {
			ImageIO.write((RenderedImage)irs.getTilePixels(pixels, x, y, zoom), "png", f);
		} catch (IOException e) {
			System.err.println("Failed to save image!" + e.getMessage());
		}
		return f;
	}
	
	public boolean onWidthBounds(int[][] array, int i) {
		return i < array[0].length && i >= 0;
	}
	
	public boolean onHeightBounds(int[][] array, int i) {
		return i < array.length && i >= 0;
	}
	
	public int getColorCode(int binaryColor) {
		return binaryColor == 0 ? Color.white.getRGB() : Color.black.getRGB();
	}
	
	
}
